package com.besttone.search;

import java.util.ArrayList;

import com.besttone.adapter.LetterListAdapter;
import com.besttone.search.CityListBaseActivity.AutoCityAdapter;
import com.besttone.search.model.City;
import com.besttone.search.sql.NativeDBHelper;
import com.besttone.search.util.Constants;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

public abstract class CityListBaseActivity extends Activity implements
		AbsListView.OnScrollListener {
	private Context mContext;
	protected ArrayList<City> citylist = new ArrayList();
	private Handler handler;
	protected ArrayAdapter<String> mAutoAdapter;
	protected EditText mAutoTextView;
	protected ArrayList<String> autoCitylist = new ArrayList<String>();
	protected String[] autoArray;
	protected String[] mCityFirstLetter;
	private TextView mDialogText;
	protected int mHotCount = 0;
	protected Cursor mHotCursor;
	private String mLetter = "";
	protected LetterListAdapter mListAdapter;
	protected ListView mListView;
	private RemoveWindow mRemoveWindow;
	private int mScroll;
	private boolean mShowing = false;
	private NativeDBHelper mDB;
	private WindowManager windowManager;
	protected AutoCityAdapter mAutoCityAdapter;
	protected ListView mAutoListView;
	
	private final TextWatcher textWatcher = new TextWatcher() {
		private int selectionEnd;
		private int selectionStart;
		private CharSequence temp;
		
		
		public void afterTextChanged(Editable paramEditable) {
			this.selectionStart = CityListBaseActivity.this.mAutoTextView
					.getSelectionStart();
			this.selectionEnd = CityListBaseActivity.this.mAutoTextView
					.getSelectionEnd();
			if (this.temp.length() > 25) {
				Toast.makeText(CityListBaseActivity.this, "temp 长度大于25", Toast.LENGTH_SHORT).show();
				paramEditable.delete(this.selectionStart - (-25 + this.temp.length()),
						this.selectionEnd);
				int i = this.selectionStart;
				CityListBaseActivity.this.mAutoTextView.setText(paramEditable);
				CityListBaseActivity.this.mAutoTextView.setSelection(i);
			}
			//获取mAutoTextView结果
			String str = temp.toString();
			Cursor localCursor = null;
			String[] arrayParm = new String[2];
			arrayParm[0] = "2";
			arrayParm[1] = (str + "%");
			autoCitylist = new ArrayList<String>();
			String[] mColumns = {"FIRST_PY", "SHORT_NAME"};
//			Log.d("selectCity", "search");
//			Log.i("arrayParm[0]", "search"+arrayParm[0]);
//			Log.i("arrayParm[q]", "search"+arrayParm[1]);
			if(Constants.isAlphabet(str)) {
				localCursor =mDB.select(mColumns, "TYPE=? AND FIRST_PY like ?",arrayParm);
			} else {
				localCursor =mDB.select(mColumns, "TYPE=? AND SHORT_NAME like ?",arrayParm);
			}
			if (localCursor!=null && localCursor.getCount()>0) {
				localCursor.moveToFirst();
				while (true) {
					if (localCursor.isAfterLast()) {
						localCursor.close();
						break;
					}
					autoCitylist.add(localCursor.getString(0) + "," + localCursor.getString(1));
//					Log.i("result", localCursor.getString(0) + "," + localCursor.getString(1));
					localCursor.moveToNext();
				}
			}
			
			autoArray = autoCitylist.toArray(new String[autoCitylist.size()]);
//			mAutoAdapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_dropdown_item_1line, autoArray);
//			mAutoTextView.setAdapter(mAutoAdapter);
//			for(int i=0;i<autoArray.length;i++)
//				Log.i("autoArray[i]", "result"+autoArray[i]);
			if(str.length()>0&&autoArray.length>0)
			{
				mAutoListView.setVisibility(View.VISIBLE);
			}
			else
			{
				mAutoListView.setVisibility(View.GONE);
			}
				
			mAutoCityAdapter.notifyDataSetChanged();
		}

		public void beforeTextChanged(CharSequence s, int start, int count,
			     int after) {
			this.temp = s;
		}

		public void onTextChanged(CharSequence s, int start, int count,
			     int after) {
		}
	};

	private void init() {
		this.mDialogText = ((TextView) ((LayoutInflater) getSystemService("layout_inflater")).inflate(R.layout.select_city_position_dialog, null));
		this.mDialogText.setVisibility(4);
		this.mRemoveWindow = new RemoveWindow();
		this.handler = new Handler();
		this.windowManager = getWindowManager();
		setAdapter();
		this.mListView.setOnScrollListener(this);
		this.mListView.setFastScrollEnabled(true);
		initAutoData();
		mAutoTextView = (EditText)this.findViewById(R.id.change_city_auto_text);
//		this.mAutoTextView.setAdapter(this.mAutoAdapter);
//		this.mAutoTextView.setThreshold(1);
		this.mAutoTextView.addTextChangedListener(this.textWatcher);
		mDB = NativeDBHelper.getInstance(this);
	}

	private void initFirstLetter() {
		setFirstletter();
		initDBHelper();
		addLocationCityFirstLetter();
		addHotCityFirstLetter();
		this.mHotCount = this.citylist.size();
		addCityFirstLetter();
	}

	private void removeWindow() {
		if (!this.mShowing)
			;
		while (true) {
			this.mShowing = false;
			this.mDialogText.setVisibility(4);
			return;
		}
	}

	public abstract void addCityFirstLetter();

	public abstract void addHotCityFirstLetter();
	
	public abstract void addLocationCityFirstLetter();

	public abstract void initAutoData();

	public abstract void initDBHelper();

	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		requestWindowFeature(1);
		setTheContentView();
		mContext = this.getApplicationContext();
		initFirstLetter();
		init();
		setListViewOnItemClickListener();
		setAutoCompassTextViewOnItemClickListener();
		
//		mAutoTextView.setAdapter(mAutoCityAdapter);
	}

	public void onScroll(AbsListView paramAbsListView, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		this.mScroll = (1 + this.mScroll);
		if (this.mScroll >= 2) {
			while (true) {
				int i = firstVisibleItem - 1 + visibleItemCount / 2;
				String str = ((City) this.mListView.getAdapter().getItem(i))
						.getFirstLetter();
				if (str == null)
					continue;
				this.mShowing = true;
				this.mDialogText.setVisibility(0);
				this.mDialogText.setText(str);
				this.mLetter = str;
				this.handler.removeCallbacks(this.mRemoveWindow);
				this.handler.postDelayed(this.mRemoveWindow, 3000L);
				return;
			}
		}

	}

	public void onScrollStateChanged(AbsListView paramAbsListView, int paramInt) {
	}

	protected void onStart() {
		super.onStart();
		WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams(
				-1, -1, 2, 24, -3);
		this.windowManager.addView(this.mDialogText, localLayoutParams);
		this.mScroll = 0;
	}

	protected void onStop() {
		super.onStop();
		try {
			this.mScroll = 0;
			this.windowManager.removeView(this.mDialogText);
			return;
		} catch (Exception localException) {
			while (true)
				localException.printStackTrace();
		}
	}
	

	public abstract void setAdapter();

	public abstract void setAutoCompassTextViewOnItemClickListener();

	public abstract void setFirstletter();

	public abstract void setListViewOnItemClickListener();

	public abstract void setTheContentView();

	final class RemoveWindow implements Runnable {
		private RemoveWindow() {
		}

		public void run() {
			CityListBaseActivity.this.removeWindow();
		}
	}
	
	public class AutoCityAdapter extends BaseAdapter
    {	
		private LayoutInflater mInflater;
    	
		public AutoCityAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
			autoArray = new String[1];
		}

    	public int getCount() 
    	{
    		return autoArray.length;
    	}


    	public Object getItem(int position) 
    	{
    		return autoArray[position];
    	}


    	public long getItemId(int position) 
    	{
    		return position;
    	}
    	


		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			convertView = mInflater.inflate(R.layout.auto_city_item, null);

    		Log.i("adapt", "getDropDownView");
    		
    		String searchRecord = autoArray[position];
    		((TextView) convertView.findViewById(R.id.auto_city_info)).setText(searchRecord);
    		
    		return convertView;
		}
    	
    }
}
